﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dycox.Linq;

namespace Dycox.Workflows
{
    [Serializable]
    public class ComponentQueryArguments : Data.DataArguments
    {
        public ComponentQueryArguments()
        {

        }

        public ComponentQueryArguments(object dataSource)
            : base(dataSource)
        {

        }

        //private string _appKey;

        //public string AppKey
        //{
        //    get { return _appKey; }
        //    set { _appKey = value; }
        //}

        private List<string> _workflowNames;

        public string WorkflowName
        {
            get
            {
                if (_workflowNames == null || _workflowNames.Count == 0)
                    return null;
                else
                    return _workflowNames[0];
            }
            set
            {
                var list = WorkflowNames;
                if (list.Count == 0) list.Add(value);
                else list[0] = value;
            }
        }

        public List<string> WorkflowNames
        {
            get
            {
                if (_workflowNames == null)
                    _workflowNames = new List<string>();
                return _workflowNames;
            }
        }

        private List<string> _stageNames;

        public List<string> StageNames
        {
            get
            {
                if (_stageNames == null)
                    _stageNames = new List<string>(10);
                return _stageNames;
            }
        }

        internal int StageNameCount
        {
            get
            {
                if (_stageNames == null) return 0;
                else return _stageNames.Count;
            }
        }

        private List<WorkflowComponentStatus> _status;

        public List<WorkflowComponentStatus> Status
        {
            get
            {
                if (_status == null) _status = new List<WorkflowComponentStatus>(10);
                return _status;
            }
        }

        internal int StatusCount
        {
            get
            {
                if (_status == null) return 0;
                else return _status.Count;
            }
        }

        private bool? _isActive;
        /// <summary>
        /// Only active steps. This argument would be ignored when query components other than <see cref="WFStep"/>.
        /// </summary>
        public bool? IsActive
        {
            get { return _isActive; }
            set { _isActive = value; }
        }

        private DateRange _creationTime;

        public DateRange CreationTime
        {
            get { return _creationTime; }
            set { _creationTime = value; }
        }

        private DateRange _finishTime;

        public DateRange FinishTime
        {
            get { return _finishTime; }
            set { _finishTime = value; }
        }

        private TimeSpanRange _duration;

        public TimeSpanRange Duration
        {
            get { return _duration; }
            set { _duration = value; }
        }


        private List<ComponentIdentifier> _componentIDs;

        public List<ComponentIdentifier> ComponentIDs
        {
            get
            {
                if (_componentIDs == null) _componentIDs = new List<ComponentIdentifier>();
                return _componentIDs;
            }
        }

        internal int ComponentIDCount
        {
            get
            {
                if (_componentIDs != null) return _componentIDs.Count;
                else return 0;
            }
        }

        private SortOrderSettings _sort;

        public SortOrderSettings Sort
        {
            get
            {
                if (_sort == null)
                {
                    _sort = new SortOrderSettings();
                    _sort["CreationTime"] = SortOrder.Descending;
                }
                return _sort;
            }
        }

        public void Reset()
        {
            //_appKey = null;
            _creationTime = _finishTime = DateRange.Empty;
            _isActive = null;
            if (_componentIDs != null)
                _componentIDs.Clear();
            if (_stageNames != null)
                _stageNames.Clear();
            if (_status != null)
                _status.Clear();
            if (_workflowNames != null)
                _workflowNames.Clear();
        }

        //private ComponentSortOrder _sort;

        //public ComponentSortOrder Sort
        //{
        //    get
        //    {
        //        if (_sort == null)
        //        {
        //            _sort = new ComponentSortOrder();
                    
        //        }

        //        return _sort;
        //    }
        //}

        //[Serializable]
        //public class ComponentSortOrder : SortOrderSettings
        //{
        //    public SortOrder CreationTime
        //    {
        //        get { return base["CreationTime"]; }
        //        set { base["CreationTime"] = value; }
        //    }

        //    public SortOrder StartTime
        //    {
        //        get { return base["StartTime"]; }
        //        set { base["StartTime"] = value; }
        //    }

        //    protected override void OnGetDefaultSortEntries(GetDefaultSortOrderEventArgs e)
        //    {
        //        e["CreationTime"] = SortOrder.Descending;
        //    }
        //}

        
    }

}
